package com.ouman.luoliluoli.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.widget.Toast;

import com.ouman.luoliluoli.AboutActivity;
import com.ouman.luoliluoli.R;

import java.util.HashMap;

/**
 * Created by jintian on 21/04/2017.
 */

public class ThemeChanger {

    public HashMap<Integer, String> themeColorMap;
    private SharedPreferences preferences;

    public ThemeChanger(Context context) {

        themeColorMap = new HashMap<>();
        themeColorMap.put(R.style.AppTheme_AirForceBlue, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorAirForceBlue)));
        themeColorMap.put(R.style.AppTheme_AliceBlue, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorAliceBlue)));
        themeColorMap.put(R.style.AppTheme_Amaranth, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorAmaranth)));
        themeColorMap.put(R.style.AppTheme_Amethyst, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorAmethyst)));
        themeColorMap.put(R.style.AppTheme_AntiqueBrass, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorAntiqueBrass)));
        themeColorMap.put(R.style.AppTheme_BattleshipGray, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorBattleshipGray)));
        themeColorMap.put(R.style.AppTheme_BluePurple, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorBluePurple)));
        themeColorMap.put(R.style.AppTheme_BrightPink, "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.themeColorBrightPink)));

    }

    public static void applyTheme(ContextThemeWrapper contextThemeWrapper, Integer styleId) {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(contextThemeWrapper);
        // 如果style id为null，这说明只需要设置即可，不需要保存
        if (styleId == null) {
            contextThemeWrapper.setTheme(preferences.getInt("globalStyle", R.style.AppTheme_NoActionBar));
        } else {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("globalStyle", styleId);
            contextThemeWrapper.setTheme(styleId);

            editor.apply();


            String[] styleNameList = contextThemeWrapper.getResources().getResourceName(styleId).split("AppTheme.");
            String realName = styleNameList[styleNameList.length - 1];
            Toast.makeText(contextThemeWrapper, "成功切换主题到 " + realName + " 下次启动生效", Toast.LENGTH_SHORT).show();
        }
    }

//    public static void getCurrentThemeColor(Context context) {
//        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
//        // 如果style id为null，这说明只需要设置即可，不需要保存
//        int style = preferences.getInt("globalStyle", R.style.AppTheme);
//        TypedValue typedValue = new TypedValue();
//
//    }

}
